package com.dhcc.DRGsHosp.application.impl;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.inject.Named;

import org.dayatang.domain.EntityRepository;
import org.dayatang.domain.InstanceFactory;
import org.dayatang.querychannel.JdbcTemplateObj;
import org.dayatang.querychannel.QueryChannelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.dhcc.DRGsHosp.application.AnnualSTDApplication;
import com.dhcc.DRGsHosp.core.domain.AnnualSTD;

@Named
@Transactional
public class AnnualSTDApplicationImpl implements AnnualSTDApplication,Serializable {
	private static final long serialVersionUID = 1L;
	
	private QueryChannelService queryChannel;
	
	@Autowired  
    private JdbcTemplateObj jdbcTemplateObj;
	
    private QueryChannelService getQueryChannelService(){
       if(queryChannel==null){
          queryChannel = InstanceFactory.getInstance(QueryChannelService.class,"queryChannel");
       }
     return queryChannel;
    }
    
    private EntityRepository getRepository() {
		EntityRepository repository = null;
		if (repository == null) {
			repository = InstanceFactory.getInstance(
					EntityRepository.class, "repository");
		}
		return repository;
	}
	public AnnualSTD getAnnualSTD(Double asYear) {
		return AnnualSTD.get(AnnualSTD.class, asYear);
	}
	
	public void creatAnnualSTD(AnnualSTD annualSTD) {
		annualSTD.save();
	}
	
	public void removeAnnualSTD(AnnualSTD annualSTD) {
		if(annualSTD != null){
			annualSTD.remove();
		}
	}
	
	public void removeAnnualSTDs(Set<AnnualSTD> annualSTDs) {
		for (AnnualSTD annualSTD : annualSTDs) {
			annualSTD.remove();
		}
	}
	
	public List<AnnualSTD> findAllAnnualSTD() {
		return AnnualSTD.findAll(AnnualSTD.class);
	}

	/* (non-Javadoc)
	 * @see com.dhcc.DRGsHosp.application.AnnualSTDApplication#findAllAnnualSTDByParam(com.dhcc.DRGsHosp.core.domain.AnnualSTD)
	 */
	@Override
	public List<AnnualSTD> findAllAnnualSTD(AnnualSTD annualSTD) {
		StringBuilder jpql = new StringBuilder("SELECT _annualSTD FROM AnnualSTD _annualSTD where 1=1 ");
		if (annualSTD.getAsYear()!=null && !"".equals(annualSTD.getAsYear())) {
			jpql.append(" and _annualSTD.asYear = '"+annualSTD.getAsYear()+"'");
		}
		if (annualSTD.getAsId()!=null && !"".equals(annualSTD.getAsId())) {
			jpql.append(" and _annualSTD.asId = '"+annualSTD.getAsId()+"'");
		}
		@SuppressWarnings("unchecked")
		List<AnnualSTD> mrList = getQueryChannelService().createJpqlQuery(jpql.toString()).list();
		return mrList;
	}

	@Override
	public Map<String, Object> findWeightsExtremum(String asId) {
		Map<String,Object> resultMap = new HashMap<>() ;
		StringBuffer strb = new StringBuffer() ;
		strb.append("  select max(e.as_weight) weightMax,min(e.as_weight) weightMin from drgs.t_annualstd e where  e.as_id='"+asId+"' ") ;
		try {
			resultMap = jdbcTemplateObj.queryForMap(strb.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resultMap ;
	}
	
}
